package com.ruoyi.common.utils;

import com.deepoove.poi.XWPFTemplate;
import org.springframework.stereotype.Component;

import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * word 模板变量替换工具
 *
 * @author lie
 * @date 2022/11/25
 */
@Component
public class WordUtil {

    /**
     * word占位用{{object}}比较完美可以填充图片
     *
     * @param filePath    文件路径
     * @param params      参数个数
     * @param outFilePath 从文件路径
     * @throws Exception 异常
     */
    public static void templateWrite(String filePath, Map<String, Object> params, String outFilePath) throws Exception {
        XWPFTemplate template = XWPFTemplate.compile(filePath).render(params);
        FileOutputStream out = new FileOutputStream(outFilePath);
        template.write(out);
        out.flush();
        out.close();
        template.close();
    }


    public static void main(String[] args) throws Exception {
        Map<String, Object> params = new HashMap<>();
        params.put("test", "四川银行");
        params.put("test1", "项目2");
        templateWrite("C:\\Users\\lie\\Desktop\\law\\test.docx", params, "D:\\uploadFile\\test1.docx");
    }

}
